﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class frmInBangdiemSV : Form
    {
        string chonlop;
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=SM;Integrated Security=True");
        public frmInBangdiemSV()
        {
            InitializeComponent();
            DataSet ds = FillDataset("Select [MalopID], [Tenlop] from [T03_Lop_hoc]");
            lkuChoplop.Properties.DataSource = ds.Tables[0];
        }

        DataSet FillDataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
            return ds;
        }

        private void frmInBangdiemSV_Load(object sender, EventArgs e)
        {
            DataSet ds = FillDataset("Select [MalopID], [Tenlop] from [T03_Lop_hoc]");
            DataRow row = ds.Tables[0].Rows[0];
            lkuChoplop.EditValue = row["MalopID"].ToString();
            chonlop = row["MalopID"].ToString();
        }

        public void get_gridviewsinhvien()
        {
            DataSet ds = FillDataset("select [MasvID], [Holot], [Ten], [Ngaysinh] from [T01_Sinh_vien] sv, [T03_Lop_hoc] lh where sv.[MalopID] = lh.[MalopID] and sv.MalopID = N'" + chonlop + "'");
            gCol_MaSV.FieldName = "MasvID";
            gCol_Holot.FieldName = "Holot";
            gCol_Ten.FieldName = "Ten";
            gCol_Ngaysinh.FieldName = "Ngaysinh";
            gridControl1.DataSource = ds.Tables[0];
        }


        private void lkuChoplop_EditValueChanged(object sender, EventArgs e)
        {
            chonlop = lkuChoplop.EditValue.ToString();
            get_gridviewsinhvien();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string masv = gridView1.GetRowCellValue(e.RowHandle, gCol_MaSV).ToString();
            txtMasv.Text = masv;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            bool ktra = dxValidationProvider1.Validate();
            if (ktra)
            {
                bool check = getRowsTable("Select MasvID from View_InbangdiemSV where MasvID = N'" + txtMasv.Text + "'");
                if (check == false)
                    MessageBox.Show("Mã số sinh viên này không tồn tại");
                else
                {
                    rptInbangdiemSV rpt = new rptInbangdiemSV();
                    rpt.DataSource = FillDataset("SELECT *, [Sotrinh] * [DiemCaoNhat] as Tong FROM View_InbangdiemSV where MasvID = N'" + txtMasv.Text + "' and Hocky between '" + txtTuhocky.Text + "' and '" + txtDenhocky.Text + "' ");
                    rpt.BindData();
                    rpt.PrintingSystem.PreviewFormEx.MdiParent = RibbonForm1.ActiveForm;
                    rpt.ShowPreview();
                }
            }
            else
                MessageBox.Show("Bạn nhập thiếu thông tin hoặc bạn nhập sai thông tin", "Quản lý sinh viên", MessageBoxButtons.OK, MessageBoxIcon.None);
       
        }

        public bool getRowsTable(string str)
        {
            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = str;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            if (dr.HasRows == true)
            {
                connection.Close();
                return true;
            }
            connection.Close();
            return false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
